package com.xiegang.servicepassengeruser.service;

import com.xiegang.internalcommon.constant.CommonStatusEnum;
import com.xiegang.internalcommon.dto.ResponseResult;
import com.xiegang.internalcommon.dto.PassengerUser;
import com.xiegang.servicepassengeruser.mapper.PassengerUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xieGang
 * @description
 * @date 2023/3/2 19:29
 **/
@Service
public class UserService {

    @Autowired
    private PassengerUserMapper passengerUserMapper;


    public ResponseResult loginOrRegister(String passengerPhone){
        System.out.println("UserService被调用 手机号："+passengerPhone);

        Map<String,Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);

        List<PassengerUser> passengerUsers = passengerUserMapper.selectByMap(map);

        //不存在用户 则插入
        if(passengerUsers.size()==0){
            PassengerUser passengerUser = new PassengerUser();
            passengerUser.setPassengerName("张三");
            passengerUser.setPassengerPhone(passengerPhone);
            passengerUser.setPassengerGender((byte)0);
            passengerUser.setState((byte)0);

            LocalDateTime now = LocalDateTime.now();
            passengerUser.setGmtCreate(now);
            passengerUser.setGmtModified(now);

            passengerUserMapper.insert(passengerUser);
            System.out.println("新增成功");
        }

        return ResponseResult.success();
    }

    public ResponseResult getUserByPhone(String passengerPhone){

        Map<String,Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);

        List<PassengerUser> passengerUsers = passengerUserMapper.selectByMap(map);
        if(passengerUsers.size()==0){
            return ResponseResult.fail(CommonStatusEnum.USER_NOT_EXSITS.getCode(),CommonStatusEnum.USER_NOT_EXSITS.getMessage());
        }

        PassengerUser passengerUser = passengerUsers.get(0);
        return ResponseResult.success(passengerUser);
    }
}
